package com.lawyer.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.persistence.EntityManager;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lawyer.entity.CaseInfo;
import com.lawyer.entity.Cases;
import com.lawyer.entity.Documents;
import com.lawyer.entity.DocumentsCaseInfo;
import com.lawyer.entity.DocumentsCases;
import com.lawyer.model.Message;
import com.lawyer.model.MessageConstant;
import com.lawyer.repository.CaseInfoRepository;
import com.lawyer.repository.CasesRepository;
import com.lawyer.repository.DocumentsCaseInfoRepository;
import com.lawyer.repository.DocumentsCasesRepository;
import com.lawyer.repository.DocumentsRepository;
import com.lawyer.service.FileUpLoadService;
import com.lawyer.uitls.BaseUtils;

@RestController
@RequestMapping(value = "/documents")
@Api(tags = "文书代写相关")
@SuppressWarnings("all")
public class DocumentsController {

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private DocumentsRepository documentsRepository;

	@Autowired
	private FileUpLoadService fileUpLoadService;
	@Autowired
	private EntityManager entityManager;

	@Autowired
	private CasesRepository casesRepository;
	@Autowired
	private CaseInfoRepository caseInfoRepository;
	
	@Autowired
	private DocumentsCaseInfoRepository documentsCaseInfoRepository;
	@Autowired
	private DocumentsCasesRepository documentsCasesRepository;

	@ApiOperation(httpMethod = "POST", value = "发布文书代写", notes = "发布文书代写")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public HttpEntity<?> save(@RequestBody Documents documents) {
		Message msg = new Message();
		try {
			CaseInfo caseInfo = caseInfoRepository.findOne(documents.getCaseInfoId());

			if (caseInfo == null) {
				msg.setCode(MessageConstant.FAILED_CODE);
				msg.setMessage("分类不能为空");
				return new ResponseEntity<>(msg, HttpStatus.OK);
			}

			if (documents.getOfferPrice() != null && documents.getOfferPrice() < 398D) {
				msg.setCode(MessageConstant.FAILED_CODE);
				msg.setMessage("服务价格最低不能低于398元");
				return new ResponseEntity<>(msg, HttpStatus.OK);
			}
			if (!BaseUtils.checkPhone(documents.getPhone())) {
				msg.setCode(MessageConstant.FAILED_CODE);
				msg.setMessage("电话号码不正确");
				return new ResponseEntity<>(msg, HttpStatus.OK);
			}
			if (StringUtils.isEmpty(documents.getIntroduce())) {
				msg.setCode(MessageConstant.FAILED_CODE);
				msg.setMessage("文书代写内容不能为空");
				return new ResponseEntity<>(msg, HttpStatus.OK);
			}

			if (StringUtils.isEmpty(documents.getUserId())) {
				msg.setCode(MessageConstant.FAILED_CODE);
				msg.setMessage("用户userId不能为空");
				return new ResponseEntity<>(msg, HttpStatus.OK);
			}

			documents.setCaseInfoId(caseInfo.getId());
			documents.setCaseInfoType(caseInfo.getCaseType());

			documents.setPay(false);
			documents.setStatus(0);
			documents = documentsRepository.save(documents);
			msg.setData(documents);

		} catch (Exception e) {
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage(MessageConstant.FAILED_MSG);
		}
		return new ResponseEntity<>(msg, HttpStatus.OK);
	}

	@ApiOperation(httpMethod = "POST", value = "发布预约上门服务", notes = "发布预约上门服务")
	@RequestMapping(value = "/door", method = RequestMethod.POST)
	public HttpEntity<?> door(@RequestBody Documents documents) {
		Message msg = new Message();
		try {
			Cases cases = casesRepository.findOne(documents.getCaseId());

			if (cases == null) {
				msg.setCode(MessageConstant.FAILED_CODE);
				msg.setMessage("分类不能为空");
				return new ResponseEntity<>(msg, HttpStatus.OK);
			}

			if (documents.getOfferPrice() != null && documents.getOfferPrice() < 2000D) {
				msg.setCode(MessageConstant.FAILED_CODE);
				msg.setMessage("服务价格最低不能低于2000元");
				return new ResponseEntity<>(msg, HttpStatus.OK);
			}
			if (!BaseUtils.checkPhone(documents.getPhone())) {
				msg.setCode(MessageConstant.FAILED_CODE);
				msg.setMessage("联系电话号码不正确");
				return new ResponseEntity<>(msg, HttpStatus.OK);
			}

			if (StringUtils.isEmpty(documents.getAppointmentAdress())) {
				msg.setCode(MessageConstant.FAILED_CODE);
				msg.setMessage("预约地址不能为空");
				return new ResponseEntity<>(msg, HttpStatus.OK);
			}

			documents.setEntrust("个人");
			documents.setDocumentsType("上门服务");

			documents.setCaseType(cases.getCaseType());
			documents.setCaseId(cases.getId());

			documents.setPay(false);
			documents.setStatus(0);
			documents = documentsRepository.save(documents);
			msg.setData(documents);

		} catch (Exception e) {
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage(MessageConstant.FAILED_MSG);
		}
		return new ResponseEntity<>(msg, HttpStatus.OK);
	}

	@ApiOperation(httpMethod = "GET", value = "涉及分类选择", notes = "涉及分类选择")
	@RequestMapping(value = "/queryCaseTypes", method = RequestMethod.GET)
	public HttpEntity<?> queryCaseTypes() {
		Message msg = new Message();
		List<DocumentsCases> list = documentsCasesRepository.findAll();
		for (DocumentsCases cases : list) {
			List<DocumentsCaseInfo> caseInfos = documentsCaseInfoRepository.findByParentId(cases.getId());
			cases.setCaseInfos(caseInfos);
		}
		msg.setData(list);
		return new ResponseEntity<>(msg, HttpStatus.OK);
	}

}
